public class ThrowTest02{
    public static void main(String[] args){
        try{
            ThrowTest.print(4);
        }catch(Exception e){
            //捕获异常后处理
            e.printStackTrace();
        }
    }
}

class ThrowTest{
    
    public static void print(int index) throws Exception{
        int[] arr = new int[]{1, 2, 3};
        if(index >= 0 && index <= 2){
            System.out.println(arr[index]);
        }else{
            //抛出异常
            throw new IndexOutOfBoundsException();
        }
    }
    
}